

#include "kemr.h"
#include <iostream>
#include "../hedmodel.h"
#include "../entity/solid.h"
#include "../entity/face.h"
#include "../entity/loop.h"
#include "../entity/vertex.h"
#include "../entity/halfedge.h"
#include "../entity/edge.h"
#include "../entity/ipoint.h"


KEMR::KEMR(HalfEdge* _h1, HalfEdge* _h2)
: m_h1( _h1 )
, m_h2( _h2 )
{
  if( _h1 == NULL || _h2 == NULL )
  {
    std::cout << "[W] Two half-edges are needed for this Euler operator work!" << std::endl;
    return;
  }
  if( _h1->getEdg() != _h2->getEdg() )
  {
    std::cout << "[W] Given half-edges must lie over the same edge!" << std::endl;
    return;
  }        
  if( _h1 == _h2 )
  {
    std::cout << "[W] Given half-edges must be different!" << std::endl;
    return;
  }      
}




void
KEMR::execute()
{
  std::cout << "\nKEMR::execute() \n";

  register HalfEdge *h3,*h4;

  Loop* ol= m_h1->getLoop();
  Loop* nl = new Loop(ol->getFace());
  Edge* killedge = m_h1->getEdg();


  h3 = m_h1->getNxt();
  m_h1->setNxt(m_h2->getNxt());
  m_h2->getNxt()->setPrv(m_h1);
  m_h2->setNxt(h3);
  h3->setPrv(m_h2);
  h4 = m_h2;
  do
  {
    h4->setWloop(nl);
  }
  while((h4 = h4->getNxt()) != m_h2);

  ol->setLedg(h3 = m_h1->delhe());
  nl->setLedg(h4 = m_h2->delhe());

  h3->getVtx()->setVedge( h3 );
  h4->getVtx()->setVedge( h4 );
//   h3->getVtx()->setVedge( (h3->getEdg()) ? h3 : NULL );
//   h4->getVtx()->setVedge( (h4->getEdg()) ? h4 : NULL );
//   h3->getNxt()->getVtx()->setVedge( (h3->getNxt()->getEdg()) ? h3->getNxt() : NULL );
//   h4->getNxt()->getVtx()->setVedge( (h4->getNxt()->getEdg()) ? h4->getNxt() : NULL );

  delete killedge;
}


void
KEMR::unExecute()
{
  std::cout << "\nKEMR::unExecute() NOT IMPLEMENTED!\n";
}
